<template>
  <div>
    <el-tree :data="menus" :props="defaultProps"
             show-checkbox node-key="catId"
             :default-expanded-keys="expandedKey"
             :expand-on-click-node="false">
      <span class="custom-tree-node" slot-scope="{ node, data }">
        <span>{{ node.label }}</span>
        <span>
          <el-button
            v-if="node.level<=2"
            type="text"
            size="mini"
            @click="() => append(data)">
            新增
          </el-button>
          <el-button
            v-if="node.level<=2"
            type="text"
            size="mini"
            @click="() => edit(data)">
            编辑
          </el-button>
          <el-button
            v-if="node.childNodes.length==0"
            type="text"
            size="mini"
            @click="() => remove(node, data)">
            删除
          </el-button>
        </span>
      </span>

    </el-tree>
    <el-dialog
      :title="title"
      :visible.sync="dialogVisible"
      :close-on-click-modal="false"
      width="30%">
      <el-form :model="category">
        <el-form-item label="分类名称">
          <el-input v-model="category.name" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="图标">
          <el-input v-model="category.icon" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="计量单位">
          <el-input v-model="category.productUnit" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
    <el-button @click="dialogVisible = false">取 消</el-button>
    <el-button type="primary" @click="submitData">确 定</el-button>
  </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      dialogTye: '', // edit,type
      title: '',
      category: {
        name: '',
        parentCid: '',
        sort: 0,
        showStatus: 1,
        catLevel: 3,
        icon: '',
        productUnit: '',
        catId: null
      },
      dialogVisible: false,
      menus: [],
      expandedKey: [],
      defaultProps: {
        children: 'children',
        label: 'name'
      },
      formLabelWidth: '120px'
    };
  },
  methods: {
    getMenus() {
      this.$http({
        url: this.$http.adornUrl('/product/category/list/tree'),
        methods: 'get'
      }).then(({data}) => {
        console.log('成功获取到数据。。。。', data.data)
        this.menus = data.data;
      })
    },
    submitData() {
      if (this.dialogTye === "add") {
        this.addCategory()
      }
      if (this.dialogTye === "edit") {
        this.editCategory()
      }
    },
    editCategory() {
      var {name, catId, catLevel, productUnit, icon} = this.category
      this.$http({
        url: this.$http.adornUrl(`/product/category/update`),
        method: 'post',
        data: this.$http.adornData({name, catId, catLevel, productUnit, icon})
      }).then(({data}) => {
        this.$message({
          message: '菜单保存成功',
          type: 'success'
        })
        this.dialogVisible = false
        // 刷新菜单
        this.getMenus()
        // 默认展开父级菜单
        this.expandedKey = [this.category.catId]
      })
    },
    append(data) {
      this.title = '添加内容'
      this.dialogTye = "add"
      this.dialogVisible = true
      this.category.catLevel = data.catLevel * 1 + 1
      this.category.parentCid = data.catId
      this.category.catId = null
      this.category.name = ''
      this.category.icon = ''
      this.category.productUnit = ''
      this.category.showStatus = 1
      this.category.sort = 0
      console.log("append", data)
    },
    /* 添加三级分类信息  */
    addCategory() {
      console.log("添加分类信息", this.category)
      this.$http({
        url: this.$http.adornUrl(`/product/category/save`),
        method: 'post',
        data: this.$http.adornData(this.category, false)
      }).then(({data}) => {
        this.dialogVisible = false,
          // 刷新菜单
          this.getMenus()
        // 默认展开父级菜单
        this.expandedKey = [this.category.parentCid]
        this.category.name = ''
        this.category.parentCid = ''
        this.category.catLevel = ''
        this.$message({
          message: '菜单保存成功',
          type: 'success'
        })
      })
    },
    edit(data) {
      this.title = '修改内容'
      this.dialogTye = "edit"
      /* 从网络中获取数据  */
      this.$http({
        url: this.$http.adornUrl(`/product/category/info/${data.catId}`),
        method: 'get',
      }).then(({data}) => {
        console.log("网络获取数据位", data)
        this.category.name = data.category.name
        this.category.catId = data.category.catId
        this.category.icon = data.category.icon
        this.category.productUnit = data.category.productUnit
        this.category.parentCid = data.category.parentCid
        this.category.catLevel = data.category.catLevel
        this.category.showStatus = data.category.showStatus
        this.category.catLevel = data.category.catLevel
        this.dialogVisible = true
      })
    },
    remove(node, data) {
      var ids = [data.catId]
      this.$confirm(`是否删除【${data.name}】 分类?`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$http({
          url: this.$http.adornUrl('/product/category/delete'),
          method: 'post',
          data: this.$http.adornData(ids, false)
        }).then(({data}) => {
          this.$message({
            message: '删除成功！',
            type: 'success'
          })
          // 刷新菜单
          this.getMenus()
          // 默认展开父级菜单
          this.expandedKey = [node.parent.data.catId]
        })
      }).catch(() => {

      });
      console.log("remove", node, data)

    }
  },
  /* 生命周期创建完成 */
  created() {
    this.getMenus();
  }
}
</script>

<style scoped>

</style>
